package com.hsytech.dijiebao;

import java.util.ArrayList;
import java.util.List;

import com.hsytech.dijiebao.account.SignInActivity;
import com.hsytech.dijiebao.base.BaseActivity;
import com.hsytech.dijiebao.base.BaseApplication;
import com.hsytech.dijiebao.chart.Frament02;
import com.hsytech.dijiebao.home.Frament00;
import com.hsytech.dijiebao.home.fragment.FragmentEmpty;
import com.hsytech.dijiebao.person.Fragment03;
import com.hsytech.dijiebao.release.Frament01;
import com.hsytech.dijiebao.utils.SharedPreferencesTools;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SynthesizerListener;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

import io.rong.imkit.RongIM;
import io.rong.imlib.RongIMClient;

public class MainActivity extends BaseActivity {
    public final static int TAXI_MYSELF_CONTACT = 103;
    public final static int HOTLE_MYSELF_CONTACT = 104;
    public final static int TAXI_HELP_CONTACT = 101;
    public final static int HOTLE_HELP_CONTACT = 100;

    public static final String KEY_TAB_INDEX = "key_tab_index";

    private static String[] sTabId = {"tab1", "tab2", "tab3", "tab4"};

    private static int[] sTabImageResId = {R.drawable.tab_home, R.drawable.tab_release, R.drawable.tab_chart,
            R.drawable.tab_person};

    private static int[] sTabLabelId = {R.string.tab_home, R.string.tab_release, R.string.tab_chart,
            R.string.tab_person};

    private static Class<?> sTabFragmentArray[] = {Frament00.class, Frament01.class, Frament02.class,
            Fragment03.class};
    private static Class<?> sTabFragmentArray1[] = {Frament00.class, FragmentEmpty.class, FragmentEmpty.class,
            FragmentEmpty.class};
    private static final int HOME_TAB = 0;
    private static final int RELESE_TAB = 1;
    private static final int CHART_TAB = 2;
    private static final int PERSON_TAB = 3;

    public static FragmentTabHost mFragmentTabHost;

    private List<TextView> mTabTextList;
    private int mCurrentTabPosition = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_master);

        initView();

    }

    private void initView() {

        if (!TextUtils.isEmpty(SharedPreferencesTools.getTOKEN(getApplicationContext()))) {

            connect(SharedPreferencesTools.getRongToken(this));

            Log.d("mylog", SharedPreferencesTools.getRongToken(this));
        }

        initBody();
    }

    private void initBody() {

        mFragmentTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);

        mFragmentTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
        mFragmentTabHost.getTabWidget().setDividerDrawable(null);

        mTabTextList = new ArrayList<TextView>();

        if (TextUtils.isEmpty(SharedPreferencesTools.getTOKEN(getApplicationContext()))) {
            for (int i = 0; i < sTabId.length; i++) {
                TabSpec tabSpec = mFragmentTabHost.newTabSpec(sTabId[i]);
                tabSpec.setIndicator(getTabItemView(i));
                Bundle bundle = new Bundle();
                bundle.putInt(KEY_TAB_INDEX, i);

                mFragmentTabHost.addTab(tabSpec, sTabFragmentArray1[i], bundle);

            }
        } else {
            for (int i = 0; i < sTabId.length; i++) {
                TabSpec tabSpec = mFragmentTabHost.newTabSpec(sTabId[i]);
                tabSpec.setIndicator(getTabItemView(i));
                Bundle bundle = new Bundle();
                bundle.putInt(KEY_TAB_INDEX, i);

                mFragmentTabHost.addTab(tabSpec, sTabFragmentArray[i], bundle);

            }
        }
        mFragmentTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {

            @Override
            public void onTabChanged(String tabId) {
                // TODO Auto-generated method stub

                mCurrentTabPosition = mFragmentTabHost.getCurrentTab();
                updateCurrentTabHostText(mFragmentTabHost);
                mFragmentTabHost.setCurrentTabByTag(tabId);

                if (TextUtils.isEmpty(SharedPreferencesTools.getTOKEN(getApplicationContext()))) {
                    showToast(R.string.msg_no_have_token);
                    Intent intent = new Intent(MainActivity.this, SignInActivity.class);
                    startActivity(intent);

                } else {


                }
//                BaseVoice.baseVoice.setVoice("henry");
//					BaseVoice.baseVoice.startSpeaking("this home");

            }

        });

    }

    private View getTabItemView(int index) {

        View view = getLayoutInflater().inflate(R.layout.layout_tab_widget, null);
        TextView tabView = (TextView) view.findViewById(R.id.tv_tab_widget);
        tabView.setText(sTabLabelId[index]);
        mTabTextList.add(tabView);
        Drawable drawable = getResources().getDrawable(sTabImageResId[index]);
        if (index == 0) {
            tabView.setTextColor(getResources().getColor(R.color.blue_first));
        }
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        tabView.setCompoundDrawables(null, drawable, null, null);
        return view;
    }

    private void updateCurrentTabHostText(FragmentTabHost tabHost) {
        int currentTabIndex = tabHost.getCurrentTab();
        for (int i = 0; i < mTabTextList.size(); i++) {
            TextView tabView = mTabTextList.get(i);
            if (tabView != null) {
                if (currentTabIndex == i) {
                    tabView.setTextColor(getResources().getColor(R.color.blue_first));
                } else {
                    tabView.setTextColor(getResources().getColor(R.color.gary01_first));
                }
            }
        }
    }

    // }
    // �ϳɼ�����
    private SynthesizerListener mSynListener = new SynthesizerListener() {
        // �Ự�����ص��ӿڣ�û�д���ʱ��errorΪnull
        public void onCompleted(SpeechError error) {
            Log.d("mylog", "voice  error:" + error);
        }

        // ������Ȼص�
        // percentΪ�������0~100��beginPosΪ������Ƶ���ı��п�ʼλ�ã�endPos��ʾ������Ƶ���ı��н���λ�ã�infoΪ������Ϣ��
        public void onBufferProgress(int percent, int beginPos, int endPos, String info) {
        }

        // ��ʼ����
        public void onSpeakBegin() {
        }

        // ��ͣ����
        public void onSpeakPaused() {
        }

        // ���Ž��Ȼص�
        // percentΪ���Ž���0~100,beginPosΪ������Ƶ���ı��п�ʼλ�ã�endPos��ʾ������Ƶ���ı��н���λ��.
        public void onSpeakProgress(int percent, int beginPos, int endPos) {
        }

        // �ָ����Żص��ӿ�
        public void onSpeakResumed() {
        }

        // �Ự�¼��ص��ӿ�
        public void onEvent(int arg0, int arg1, int arg2, Bundle arg3) {
        }
    };

    /**
     * ���������Ʒ�����������
     *
     * @param token
     */
    private void connect(String token) {

        if (getApplicationInfo().packageName.equals(BaseApplication.getCurProcessName(getApplicationContext()))) {

            /**
             * IMKit SDK���õڶ���,�����������������
             */
            RongIM.connect(token, new RongIMClient.ConnectCallback() {

                /**
                 * Token ���������ϻ�������Ҫ����Ϊ Token �Ѿ����ڣ�����Ҫ�� App Server ��������һ���µ�
                 * Token
                 */
                @Override
                public void onTokenIncorrect() {

                    Log.d("mylog", "--onTokenIncorrect");
                }

                /**
                 * �������Ƴɹ�
                 *
                 * @param userid
                 *            ��ǰ token
                 */
                @Override
                public void onSuccess(String userid) {

                    Log.d("mylog", "--onSuccess" + userid);
                }

                /**
                 * ��������ʧ��
                 *
                 * @param errorCode
                 *            �����룬�ɵ����� �鿴�������Ӧ��ע��
                 */
                @Override
                public void onError(RongIMClient.ErrorCode errorCode) {

                    Log.d("mylog", "--onError" + errorCode);
                }
            });
        }
    }

}
